/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Classes.Cliente;
import Controle.ControleCliente;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Eduardo
 */
public class CadastroCliente extends javax.swing.JDialog {

    /**
     * Creates new form CadastroMensalista
     */
    public CadastroCliente() {
        initComponents();
        setModal(true);
        msgErro.setVisible(false);
        atualizaTabela();
    }

    public static void atualizaTabela() {
        try {
            ControleCliente cc = new ControleCliente();
            ArrayList<Cliente> cl = new ArrayList<Cliente>();
            cl = cc.listaClientes();
            Object[][] tb = new Object[cl.size()][3];
            for (int i = 0; i < cl.size(); i++) {
                tb[i][0] = cl.get(i).getCodigo();
                tb[i][1] = cl.get(i).getNome();
                tb[i][2] = cl.get(i).getCpf();
            }
            tabela.setModel(new javax.swing.table.DefaultTableModel(tb,
                    new String[]{
                        "Código", "Nome do Cliente", "CPF"
                    }) {

                Class[] types = new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean[]{
                    false, false, false
                };

                public Class getColumnClass(int columnIndex) {
                    return types[columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
            });
            tabela.repaint();
        } catch (SQLException ex) {
            Logger.getLogger(CadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        msgErro = new javax.swing.JLabel();
        botaoIncluirAtiv = new javax.swing.JButton();
        textoTituloJanela = new javax.swing.JLabel();
        jBotaoVeiculosClientes = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Clientes Mensalistas - Power Park");
        setResizable(false);

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome do Cliente", "CPF"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setColumnSelectionAllowed(true);
        tabela.getTableHeader().setReorderingAllowed(false);
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabela.getColumnModel().getColumn(0).setMinWidth(70);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(70);
        tabela.getColumnModel().getColumn(0).setMaxWidth(70);

        msgErro.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        msgErro.setForeground(new java.awt.Color(255, 0, 0));
        msgErro.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        msgErro.setText("Selecione algum item na tabela");

        botaoIncluirAtiv.setText("Incluir Novo Cliente");
        botaoIncluirAtiv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoIncluirAtivActionPerformed(evt);
            }
        });

        textoTituloJanela.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        textoTituloJanela.setText("Cadastro de Clientes Mensalistas");

        jBotaoVeiculosClientes.setText("Cadastrar Veículos para o Cliente Selecionado");
        jBotaoVeiculosClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBotaoVeiculosClientesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textoTituloJanela)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(msgErro, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBotaoVeiculosClientes)
                    .addComponent(botaoIncluirAtiv, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(msgErro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaoIncluirAtiv)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBotaoVeiculosClientes)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textoTituloJanela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(72, 72, 72))))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-704)/2, (screenSize.height-343)/2, 704, 343);
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        msgErro.setVisible(false);
    }//GEN-LAST:event_tabelaMouseClicked

    private void botaoIncluirAtivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoIncluirAtivActionPerformed
        msgErro.setVisible(false);
        NovoCliente i1 = new NovoCliente(-1);
        i1.setVisible(true);
        i1.repaint();
    }//GEN-LAST:event_botaoIncluirAtivActionPerformed

    private void jBotaoVeiculosClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBotaoVeiculosClientesActionPerformed
        int linha = tabela.getSelectedRow();
        if (linha >= 0) {
            String cod = tabela.getModel().getValueAt(linha, 0).toString();
            NovoVeiculo nv = new NovoVeiculo(cod);
            nv.setVisible(true);
            nv.repaint();
        } else {
            msgErro.setVisible(true);
        }
    }//GEN-LAST:event_jBotaoVeiculosClientesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoIncluirAtiv;
    private javax.swing.JButton jBotaoVeiculosClientes;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JLabel msgErro;
    private static javax.swing.JTable tabela;
    private javax.swing.JLabel textoTituloJanela;
    // End of variables declaration//GEN-END:variables
}
